package com.good.jzhouse.tools;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author: xiaolei
 * @Date: 2021/3/13 14:00
 */
@Component
public class ApplicationUtil implements ApplicationContextAware {
    public static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            context = applicationContext;
        System.out.println("ApplicationUtils-------->context:" + context);
    }
    //根据名字拿组件
    public static Object getBeansByName(String Beanname){
         return context.getBean(Beanname);
    }

    //根据类的类型拿组件
    public static  <T> T getBeansByClass(Class<T> Type){

        System.out.println("APPlication:" + context);
        return  context.getBean(Type);
    }
}
